us_renew <- read_csv(here("data", "renewables_cons_prod.csv")) %>% 
  clean_names()
# make the description all lower case 
# only keeo obs where "description" var contains "consumption"
# remove any obs where "description" contains "total"

renew_clean <- us_renew %>% 
  mutate(description = str_to_lower(description)) %>% 
  filter(str_detect(description, pattern = "consumption")) %>% 
  filter(!str_detect(description, pattern = "total")) # get rid of total

Convert ‘yyyymm’ column to a date

renew_date <- renew_clean %>% 
  mutate(yr_mo_day = lubridate::parse_date_time(yyyymm, "ym")) %>% 
  mutate(month_sep = yearmonth(yr_mo_day)) %>%  # pull just the year and month from that column 
  mutate(value = as.numeric(value)) %>% 
  drop_na(month_sep, value)

# make a version where I have the month and year in separate column 
renew_parsed <- renew_date %>% 
  mutate(month = month(yr_mo_day, label = T)) %>% 
  mutate(year = year(yr_mo_day))

Look at it

# renew_gg <- ggplot(data = renew_date, aes(x = month_sep, y = value)) + 
#   geom_line()
# 
# renew_gg

#这个图非常不说明问题,看下面
renew_gg <- ggplot(data = renew_date, aes(x = month_sep, y = value, group = description)) +
  geom_line(aes(color = description))

renew_gg

Updatingcolors with paletteer palettes

renew_gg +
  scale_color_paletteer_d("calecopal::conifer") # view(palettes_d_names)这行码给你看所有颜色的组合

Coerse renew_parse to a tibble

renew_ts <- as_tsibble(renew_parsed, key = description, index = month_sep)

Let’s look at our ts data in a couple different ways

renew_ts %>% autoplot(value)

renew_ts %>% gg_subseries(value)

renew_ts %>% autoplot(value)

renew_ts %>% gg_subseries(value)

# renew_ts %>% gg_season(value) 这个用不了就用gg自己做
ggplot(data = renew_parsed, aes(x = month, y = value, group = year)) +
  geom_line(aes(color = year)) +
  facet_wrap(~ description, 
             ncol = 1, 
             scales = "free", 
             strip.position = "right")

look at hydroelectric energy consumption

hydro_ts <- renew_ts %>% 
  filter(description == "hydroelectric power consumption")

# Explore: 
hydro_ts %>% autoplot(value)

hydro_ts %>% gg_subseries(value)

# hydro_ts %>% gg_season(value)

ggplot(hydro_ts, aes(x = month, y = value, group = year)) + 
  geom_line(aes(color = year))

what if i want quarterly average consumption for hydro?

hydro_quarterly <- hydro_ts %>% 
  index_by(year_qu = ~ yearquarter(.)) %>% # monthly aggregates
  summarise(
    avg_consumption = mean(value)
  )

head(hydro_quarterly)
## # A tsibble: 6 x 2 [1Q]
##   year_qu avg_consumption
##     <qtr>           <dbl>
## 1 1973 Q1            261.
## 2 1973 Q2            255.
## 3 1973 Q3            212.
## 4 1973 Q4            225.
## 5 1974 Q1            292.
## 6 1974 Q2            290.

decompose that hydro_ts

dcmp <- hydro_ts %>% 
  model(STL(value~season(window = 5)))

components(dcmp) %>% autoplot()

hist(components(dcmp)$remainder)

now look at ACF fuction

hydro_ts %>% 
  ACF(value) %>% 
  autoplot()

DANGER DANGER

hydro_model <- hydro_ts %>% 
  model(
    ARIMA(value)
  ) %>% 
  fabletools::forecast(h = "4 years")

hydro_model %>%  autoplot(filter(hydro_ts, year(month_sep) > 2010))

make a world map

world <- read_sf(dsn = here("data", "TM_WORLD_BORDERS_SIMPL-0.3-1"),
                 layer = "TM_WORLD_BORDERS_SIMPL-0.3")

mapview(world)